<template>
  <div class="report-upload-step">
    <div class="step-content">
      <div class="upload-area">
        <el-upload
          class="upload-container"
          drag
          multiple
          :auto-upload="false"
          :on-change="handleChange"
          accept=".pdf,.doc,.docx"
        >
          <el-icon class="el-icon--upload"><document-add /></el-icon>
          <div class="el-upload__text">
            将文件拖到此处，或<em>点击上传</em>
          </div>
          <template #tip>
            <div class="el-upload__tip">
              支持上传 PDF、Word 格式的病例报告文件
            </div>
          </template>
        </el-upload>
      </div>

      <div class="file-list" v-if="store.reportFiles.length > 0">
        <h3>已上传文件</h3>
        <el-table :data="store.reportFiles" style="width: 100%">
          <el-table-column prop="name" label="文件名" />
          <el-table-column prop="uploadTime" label="上传时间">
            <template #default="scope">
              {{ formatDate(scope.row.uploadTime) }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="180">
            <template #default="scope">
              <el-button
                link
                type="primary"
                size="small"
                @click="handlePreview(scope.row)"
              >
                预览
              </el-button>
              <el-button
                link
                type="danger"
                size="small"
                @click="handleDelete(scope.$index)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { DocumentAdd } from '@element-plus/icons-vue'
import { useWorkflowStore } from '@/stores/workflow'
import { ElMessage } from 'element-plus'

const store = useWorkflowStore()

const formatDate = (dateString: string) => {
  return new Date(dateString).toLocaleString()
}

const handleChange = (file: any) => {
  // 模拟文件上传成功
  store.reportFiles.push({
    id: String(Date.now()),
    name: file.name,
    path: URL.createObjectURL(file.raw),
    uploadTime: new Date().toISOString()
  })
  ElMessage.success('文件上传成功！')
}

const handlePreview = (file: { path: string }) => {
  // 在新窗口中打开文件预览
  window.open(file.path, '_blank')
}

const handleDelete = (index: number) => {
  store.reportFiles.splice(index, 1)
}
</script>

<style scoped>
.report-upload-step {
  height: 100%;
  width: 100%;
  position: relative;
  overflow: auto;
}

.step-content {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 20px;
  box-sizing: border-box;
}

.upload-area {
  display: flex;
  justify-content: center;
  width: 100%;
}

.upload-container {
  width: 100%;
  max-width: 600px;
}

.file-list {
  flex: 1;
  background: #fff;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
}

.file-list h3 {
  margin-bottom: 20px;
  font-size: 16px;
  color: #303133;
}

:deep(.el-upload-dragger) {
  width: 100%;
}

:deep(.el-upload__tip) {
  margin-top: 10px;
  color: #909399;
}

:deep(.el-table) {
  margin-bottom: 20px;
}
</style> 